package Objetos 
{
	
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard
	import Levels.Level
	
	/**
	 * ...
	 * @author Gabriel Ribeiro
	 */
	public class Nave extends MovieClip
	{
		protected var NB_velocidadeY			:Number;
		protected var NB_velocidadeX			:Number;
		
		protected var BO_dir 					:Boolean;
		protected var BO_esq					:Boolean;
		protected var BO_cima					:Boolean;
		protected var BO_baixo					:Boolean;
		public	  var BO_morreu					:Boolean;
		public 	  var BO_pegouUpgrade			:Boolean;
		
		protected var MC_tiro					:Tiro;
		
		
		
		public function Nave() 
		{
			NB_velocidadeY = 10;
			NB_velocidadeX = 10;
			
			Main.getStage().addEventListener(KeyboardEvent.KEY_DOWN, pressionar, false, 0, true)
			Main.getStage().addEventListener(KeyboardEvent.KEY_UP, soltar, false, 0, true)
		}
		
		public function update():void
		{
			controlarNave();
			
			if (this.x >= stage.stageWidth - this.width/2)
			{
				this.x = stage.stageWidth - this.width/2
			}
			
			if (this.x <= + this.width/2)
			{
				this.x = + this.width/2
			}
			
			if (this.y >= stage.stageHeight - this.height/2)
			{
				this.y = stage.stageHeight - this.height/2
			}
			
			if (this.y <= + this.height / 2)
			{
				this.y = + this.height/2
			}
		}
		
	 
		public function pressionar (e:KeyboardEvent):void
		{
		
			switch(e.keyCode)
			{
				
				case Keyboard.UP:
				BO_cima = true;
				break;
				
				case Keyboard.DOWN:
				BO_baixo = true;
				break;
				
				case Keyboard.RIGHT:
				BO_dir = true;
				break;
				
				case Keyboard.LEFT:
				BO_esq = true;
				break;
				
				case Keyboard.SPACE :
				criarTiro();
				break;
			  	
			}
		}
		
				
		public function soltar (e:KeyboardEvent):void
		{
			switch(e.keyCode)
			{
				case Keyboard.UP:
				BO_cima = false;
				break;
				
				case Keyboard.DOWN:
				BO_baixo = false;
				break;
				
				case Keyboard.RIGHT:
				BO_dir = false;
				break;
				
				case Keyboard.LEFT:
				BO_esq = false;
			}
		} 
		
		public function controlarNave():void 
		{
			if (BO_cima)
			{
				this.y -= NB_velocidadeY;
			}
			
			if (BO_baixo)
			{
				this.y += NB_velocidadeY;
			}
			
			if (BO_dir)
			{
				this.x += NB_velocidadeX;
			}
			
			if (BO_esq)
			{
				this.x -= NB_velocidadeX;
			}
			
		}
		
		public function criarTiro():void
		{
			MC_tiro = new Tiro();
			MC_tiro.x = this.x;
			MC_tiro.y = this.y - 35;
			Main.getInstance().MC_tela.addChild(MC_tiro);
			Level.AR_tiros.push(MC_tiro);
		}
		
	}

}